<?php

namespace App\Repositories\Personal;

use App\Exceptions\API\APIException;
use App\Exceptions\API\BadRequest;
use App\Models\Absence;
use App\Models\Attendance;
use App\Models\Egress;
use App\Models\Transportation;
use App\Models\User;
use App\Models\WorkTime;
use App\Repositories\Attendance\AttendanceFilter;
use Carbon\Carbon;

class PersonalDBRepository implements PersonalRepository
{

    public function getTotal($user_id, PersonalFilter $personalFilter)
    {
        $user = User::find($user_id);
        $absenceTotal = Absence::where('user_id', $user_id)->get()->count();
        $egressTotal = Egress::where('user_id', $user_id)->get()->count();
        $transportTotal = Transportation::where('user_id', $user_id)->get()->count();
        $overTime = 0;
        $work = WorkTime::where('work_type',2);
        if ($personalFilter->getStartTime() && $personalFilter->getEndTime()) {
            $work->where('start_time', '>=', $personalFilter->getStartTime(). ' 00:00:00')
                ->where('start_time', '<=', $personalFilter->getEndTime(). ' 23:00:00');
        }
        $work->get();
        $work->map(function($item) use ($user, &$overTime){
            $isAttendance = Attendance::where('attendance_no', $user->attendance_no)
                ->where('name', $user->name)
                ->where('register_time','like', '%'. substr($item->start_time, 0, 10). '%')
                ->get()
                ->isEmpty();
            if (!$isAttendance){
                $overTime++;
            }
        });
        return ['absence_total' => $absenceTotal, 'egress_total'=> $egressTotal,'over_time'=>$overTime, 'transport_total'=>$transportTotal];

    }

    public function getTrend($user_id, PersonalFilter $personalFilter)
    {
        $user = User::find($user_id);

        $startTime = Carbon::parse(Carbon::parse('-7 days')->toDateString())->toDateTimeString();
        $endTime = Carbon::today()->toDateTimeString();


        if ($personalFilter->getStartTime() && $personalFilter->getEndTime()){
            $startTime = $personalFilter->getStartTime();
            $endTime = $personalFilter->getEndTime();
        }

        $work = WorkTime::where('start_time', '>=', $startTime. ' 00:00:00')
                ->where('start_time', '<=', $endTime. ' 23:00:00')->get()->toArray();

        $trends = [];
        foreach ($work as $v) {
            $attendce = Attendance::where('attendance_no', $user->attendance_no)
                ->where('name', $user->name)
                ->where('register_time','like', '%'. substr($v['start_time'], 0, 10). '%')
                ->get();
            if ($attendce->isEmpty()) {
                $trends[substr($v['start_time'], 0, 10)] = 0.0;
                continue;
            }
            $sortRes = $attendce->sortBy('register_time');
            $startTime = $sortRes->first();
            $endTime = $sortRes->pop();
            $minute=round((float)((strtotime($endTime->register_time)-strtotime($startTime->register_time))%86400/3600),1);
            $trends[substr($v['start_time'], 0, 10)] = $minute;
        }
        return $trends;
    }

}
